package team.seekers.catering.controller.api;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.entity.vo.info.OrderInfoVO;
import team.seekers.catering.entity.vo.list.OrderListVO;
import team.seekers.catering.service.FoodService;
import team.seekers.catering.service.OrderService;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author hbc
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private FoodService foodService;

    @Autowired
    private OrderService orderService;

    /**
     * 新建订单
     */
    @GetMapping("/create/{foodId}")
    public R create(@PathVariable("foodId") String foodId){
        if(!foodService.isExist(foodId)){
            return R.fail(ResultCode.GET_FAIL).message("食物不存在，请联系客服");
        }
        String orderId = orderService.create(foodId, CateringUtil.getUserId());
        return R.success().data("id",orderId);
    }

    /**
     * 完成订单
     */
    @GetMapping("/end/{orderId}")
    public R end(@PathVariable("orderId") String orderId){
        if(!orderService.isExist(orderId)){
            return R.fail(ResultCode.GET_FAIL).message("订单不存在，请联系客服");
        }
        orderService.end(orderId);
        return R.success();
    }

    /**
     * 查询今日订单
     */
    @GetMapping("/today")
    public R today(){
         OrderInfoVO orderInfoVO = orderService.getToday();
        return R.success().data("info",orderInfoVO);
    }

    /**
     * 厨师端查询今日未完成订单
     */
    @GetMapping("/todayOrders")
    public R todayOrders(){
        List<OrderListVO> list = orderService.getTodayOrders();
        return R.success().data("rows",list);
    }

    /**
     * 学生端查询历史订单
     */
    @GetMapping("/oldOrders")
    public R oldOrders(){
        List<OrderListVO> list = orderService.getOldOrders();
        return R.success().data("rows",list);
    }

    /**
     * 查询历史订单
     */
    @GetMapping("/page/{current}/{limit}")
    public R page(@PathVariable("current") Integer current,
                  @PathVariable("limit") Integer limit){
        return R.success();
    }

}

